﻿using ContosoUniversity.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;

namespace Commission
{
    public class TaskConfig
    {
        private static CacheItemRemovedCallback OnCacheRemove = null;

        public static void RegisterTasks()
        {
            AddTask("DoStuff", 60);
        }

        private static void AddTask(string name, int seconds)
        {
            OnCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved);

            HttpRuntime.Cache.Insert(name, seconds, null,
                DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration,
                CacheItemPriority.NotRemovable, OnCacheRemove);
        }

        private static void CacheItemRemoved(string k, object v, CacheItemRemovedReason r)
        {
            // do stuff here if it matches our taskname, like WebRequest
            CustomerController cust = new CustomerController();
            cust.GetPoint(0);

            // re-add our task so it recurs
            AddTask(k, Convert.ToInt32(v));
        }
    }
}